from rest_framework import serializers

from social.payment.models import Wallet, Product, WalletRecord
from social.utils.serializers import HashidField
from social.utils.shortcut import get_object_or_none


class WalletSerializers(serializers.ModelSerializer):
    name_auth = serializers.SerializerMethodField(method_name='is_name_auth')

    def is_name_auth(self, instance):
        from social.user.models import NameAuth
        name_auth = get_object_or_none(NameAuth, user=instance.user)
        return (name_auth.status if name_auth else 0) == 1

    class Meta:
        model = Wallet
        fields = ('balance', 'income', 'name_auth', 'alipay')


class WalletRecordSerializer(serializers.ModelSerializer):
    class Meta:
        model = WalletRecord
        fields = ('title', 'change', 'created_at')


class CoinProductSerializer(serializers.ModelSerializer):
    id = HashidField(model=Product)

    class Meta:
        model = Product
        fields = ('id', 'price', 'coin')


class VIPProductSerializer(serializers.ModelSerializer):
    id = HashidField(model=Product)
    month_price = serializers.SerializerMethodField()
    desc = serializers.SerializerMethodField()

    def get_desc(self, instance):
        if instance.month == 1:
            return "=🥤x2"
        return f"节省{int((instance.origin_price - instance.price) * 100 / instance.origin_price)}%"

    def get_month_price(self, instance):
        return int(instance.price / instance.month)

    class Meta:
        model = Product
        fields = ('id', 'price', 'month', 'month_price', 'desc', 'title')
